<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>申购人，查看申购单</title>
    <script type="text/javascript">
      var rejectPurchaseWin;
      var isManager = '<%=session[:is_dept_manager]%>'
      var dept_auditing_status = <%=params[:dept_auditing_status]%>;
      var rejectWin;
      var passWin;
      Ext.onReady(function(){
        var status = '<%=params[:auditing_status]%>'
        var orderNumber = new Ext.form.NumberField({
          fieldLabel: '申购序号',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'order_number',
          anchor: '90%'
        });

        // simple array store
        var purchaseTypeStore = new Ext.data.ArrayStore({
          //fields: ['value', 'text'],
          fields: ['id','text'],
          data : purchaseTypeDataArray
        });

        var purchaseType = new Ext.form.ComboBox({
          fieldLabel: '申购单类型',
          store: purchaseTypeStore,
          valueField:'id',
          displayField:'text',
          typeAhead: true,
          mode: 'local',
          triggerAction: 'all',
          selectOnFocus: true,
          hiddenName:'purchase_type',
          allowBlank: false,
          listWidth: 120,
          readOnly:true,
          anchor: '90%'
        });

        var category = new Ext.form.TextField({
          fieldLabel: '产品分类',
          allowBlank: false,
          name: 'category',
          anchor: '90%'
        });
        
        var purchaseUnit = new Ext.form.TextField({
          fieldLabel: '申购单位',
          allowBlank: false,
          blankText: "申购单位不能为空",
          name: 'purchase_unit',
          anchor: '90%',
          readOnly: true,
          value:"<%=session[:department_name]%>"
        });
        
        //申购时间
        var actionDate = new Ext.form.DateField({
          fieldLabel: '申购时间',
          name: 'action_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          readOnly: true,
          anchor: '90%'
        });


        var subDept = new Ext.form.TextField({
          fieldLabel: '实验室(科室)',
          name: 'sub_dept',
          allowBlank: false,
          readOnly: true,
          anchor: '90%'
        });

        var manager = new Ext.form.TextField({
          fieldLabel: '管理人',
          name: 'manager',
          anchor: '90%',
          readOnly: true,
          value:'<%=session[:applicant_name]%>'
        });

        var fundingSource = new Ext.form.TextField({
          fieldLabel: '经费来源',
          name: 'funding_source',
          allowBlank: false,
          readOnly: true,
          anchor: '90%'
        });

        var fundingAccount = new Ext.form.TextField({
          fieldLabel: '经费账号',
          name: 'funding_account',
          readOnly: true,
          anchor: '90%'
        });

        var fundingNature = new Ext.form.TextField({
          fieldLabel: '经费性质',
          name: 'funding_nature',
          readOnly: true,
          disabled: true,
          anchor: '90%'
        });

        var productName = new Ext.form.TextField({
          fieldLabel: '产品名称',
          name: 'product_name',
          allowBlank: false,
          readOnly: true,
          anchor: '90%'
        });

        var serviceLift = new Ext.form.NumberField({
          fieldLabel: '需用时间',
          name: 'service_lift',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          readOnly: true,
          disabled: true,
          anchor: '90%'
        });


        var quantity = new Ext.form.NumberField({
          fieldLabel: '台件数',
          name: 'quantity',
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          readOnly: true,
          anchor: '90%'
        });

        var brand = new Ext.form.TextField({
          fieldLabel: '品牌',
          name: 'brand',
          readOnly: true,
          anchor: '90%'
        });

        var model = new Ext.form.TextField({
          fieldLabel: '型号',
          allowBlank: false,
          readOnly: true,
          name: 'model',
          anchor: '90%'
        });

        var specification = new Ext.form.TextField({
          fieldLabel: '型号规格',
          name: 'specification',
          readOnly: true,
          anchor: '95%'
        });

        var manufacturer = new Ext.form.TextField({
          fieldLabel: '生产单位',
          name: 'manufacturer',
          readOnly: true,
          anchor: '90%'
        });

        var estimatedPrice = new Ext.form.NumberField({
          fieldLabel: '预估单价',
          name: 'estimated_price',
          readOnly: true,
          allowBlank: false,
          allowDecimals: false,
          allowNegative: false,
          anchor: '90%'
        });
        
        var estimatedAmount = new Ext.form.NumberField({
          fieldLabel: '预估总额',
          name: 'estimated_amount',
          readOnly: true,
          allowBlank: false,
          allowNegative: false,
          anchor: '90%'
        });

        var reject_reason = new Ext.form.TextField({
          name: 'reject_reason',
          anchor: '90%',
          readOnly: true,
          hidden:true
        });

        var dept_reject = new Ext.form.TextField({
          name: 'dept_reject',
          anchor: '90%',
          readOnly: true,
          hidden:true
        });

        //计算总额
        estimatedPrice.on('blur',countEstimatedAmount);
        quantity.on('blur',countEstimatedAmount);

        function countEstimatedAmount(){
          estimatedAmount.setValue(quantity.getValue()*estimatedPrice.getValue());
        }
        
        var qualification = new Ext.form.TextArea({
          fieldLabel: '主要技术指标',
          name: 'qualification',
          height:80,
          readOnly: true,
          anchor: '97%'
        });

        var serviceRequest = new Ext.form.TextArea({
          fieldLabel: '服务要求',
          name: 'service_request',
          readOnly: true,
          height:80,
          anchor: '97%'
        });

        //供货时间
        var supplyDate = new Ext.form.DateField({
          fieldLabel: '供货时间',
          name: 'supply_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: false,
          readOnly: true,
          blankText: "申请日期必须选择",
          emptyText: '请选择日期...',
          anchor: '95%'
        });

        var supplyAddress = new Ext.form.TextField({
          fieldLabel: '供货地点',
          readOnly: true,
          name: 'supply_address',
          anchor: '96%'
        });

        var vendorName = new Ext.form.TextField({
          fieldLabel: '供应商',
          name: 'vendor_name',
          allowBlank: false,
          anchor: '95%'
        });

        var phone = new Ext.form.TextField({
          fieldLabel: '申请人电话',
          name: 'phone',
          allowBlank: false,
          anchor: '90%'
        });

        var mobile = new Ext.form.TextField({
          fieldLabel: '申请人手机',
          name: 'mobile',
          allowBlank: false,
          anchor: '90%'
        });
        
        var reason = new Ext.form.TextArea({
          fieldLabel: '申请理由及详细用途',
          allowBlank: false,
          readOnly: true,
          name: 'reason',
          height:80,
          anchor: '97%'
        });

        var rejectReasonButton = new Ext.Button({
          text:'查看驳回原因',
          handler:function(){
            var rejectReason = reject_reason.getValue()
            var deptReject = dept_reject.getValue();
            if(deptReject != ""){
              Ext.MessageBox.alert('驳回原因', '部门驳回：'+deptReject);
            }else if(rejectReason != ""){
              Ext.MessageBox().alert('驳回原因', '设备处驳回：'+rejectReason)
            }
            
          }
        })

        var passButton = new Ext.Button({
          text:'通过',
          handler:function(){
            Ext.MessageBox.confirm('<font color=red>警告</font>','您确定要审批通过申购单么？',function (button){
              if(button=='yes'){
                Ext.Ajax.request({
                  url : '/purchase/deptPassPurchase?purchase_id=<%=params[:id]%>',
                  success : function (response, req) {
                    if (Ext.util.JSON.decode(response.responseText).info === "success") {
                      purchaseViewWin.destroy();
                    } else {
                      Ext.MessageBox.alert('提示', Ext.util.JSON.decode(response.responseText).info);
                    }
                  },
                  failure : function (result, request){
                  },
                  method : 'POST'
                });
              }
            });
          }
        })

        var rejectButton = new Ext.Button({
          text:'驳回',
          handler:function(){
            rejectWin = new Ext.Window({
              layout: 'fit',
              width: 417,
              height: 140,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              autoScroll: true,
              //modal: true,
              title: '驳回原因',
              autoLoad: {
                url: "/purchase/rejectReason?id=<%=params[:id]%>&type=dept",
                scripts: true
              }
            })
            rejectWin.resizable = false;
            rejectWin.show();
          }
        })

        rejectReasonButton.setVisible(false);
        rejectButton.setVisible(false);
        passButton.setVisible(false);
        if(status =='3'){
          rejectReasonButton.setVisible(true);
        }
        
        if(isManager == 'yes' && status == '0'){
          rejectButton.setVisible(true);
          passButton.setVisible(true);
        }

        //判断，如果即是部门领导，此订单又未审批，则可以直接审批
        if (isManager == 'yes' && dept_auditing_status == 0){
          rejectButton.setVisible(true);
          passButton.setVisible(true);
        }

        var purchaseViewForm = new Ext.FormPanel({
          labelWidth: 75, // label settings here cascade unless overridden
          //url:'/purchase/create',
          frame:true,
          labelAlign: 'right',
          buttonAlign: 'center',
          //title: '添加申购单',
          bodyStyle:'padding:5px 5px 0',
          width: 760,
          //height:300,
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'order_number'
            }, {
              name: 'manager'
            }, {
              name: 'purchase_unit'
            }, {
              name: 'funding_source'
            }, {
              name: 'funding_account'
            }, {
              name: 'funding_nature'
            }, {
              name: 'action_date'
            }, {
              name: 'quantity'
            }, {
              name:'brand'
            }, {
              name: 'service_lift'
            }, {
              name: 'sub_dept'
            }, {
              name: 'product_name'
            }, {
              name: 'model'
            }, {
              name: 'specification'
            }, {
              name: 'manufacturer'
            }, {
              name: 'unit_price'
            }, {
              name: 'estimated_price'
            }, {
              name: 'estimated_amount'
            }, {
              name: 'qualification'
            }, {
              name: 'reason'
            },{
              name:'reject_reason'
            },{
              name:'dept_reject'
            },{
              name:'vendor_name'
            },{
              name:'phone'
            },{
              name:'mobile'
            },{
              name:'purchase_type'
            }]),
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [purchaseUnit]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [subDept]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manager]
                }, {
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [purchaseType]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingSource]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingAccount]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [fundingNature]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [serviceLift]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [productName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [brand]
                },{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [specification]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [manufacturer]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [quantity]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedPrice]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [estimatedAmount]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [vendorName]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [phone]
                },{
                  columnWidth: .25,
                  layout: 'form',
                  border: false,
                  items: [mobile]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [qualification]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [reason]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: 1,
                  layout: 'form',
                  border: false,
                  items: [reject_reason,dept_reject]
                }]
            }],
          buttons: [rejectReasonButton,passButton,rejectButton,{
              text: '关闭',
              handler: function(){
                purchaseViewWin.destroy();
              }
            }]
        });
        purchaseViewForm.form.load({
          url: '/purchase/edit?id=<%=params[:id]%>',
          waitMsg: '正在载入数据...',
          success: function(form, action){
          },
          failure: function(form, action){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        purchaseViewForm.render('purchase_view_form');
      })
    </script>
  </head>
  <body>
    <div id="purchase_view_form"></div>
  </body>
</html>
